package com.i2bt.adapter;

import com.i2bt.R;
import com.i2bt.app.files.FilesSharedActivity;
import com.i2bt.bean.FriendInfoBean;
import com.i2bt.util.RandomImageUtil;

import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 
 * Title : 好友分享列表适配器类
 *
 * Description : 对好友分享列表显示进行控制
 *
 * CreatedTime : 2014-9-1
 *
 * @author wuliao & kakaxi & yi
 *
 * @version 1.0
 *
 */

public class FilesSharedListAdapter extends BaseAdapter {
	
	private FilesSharedActivity activity; // 好友分享选择活动类
	
	private List<FriendInfoBean> friendSharedData; // 好友信息
	
	private SparseBooleanArray friendSharedBooleanArr; // 好友分享选择布尔数组
	
	public FilesSharedListAdapter(FilesSharedActivity activity, List<FriendInfoBean> friendSharedData) {
		this.activity = activity;
		if (friendSharedData == null) {
			friendSharedData = new ArrayList<FriendInfoBean>();
		}
		this.friendSharedData = friendSharedData;
		friendSharedBooleanArr = new SparseBooleanArray();
	}

	@Override
	public int getCount() {
		return friendSharedData.size();
	}

	@Override
	public Object getItem(int position) {
		return friendSharedData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(activity).inflate(
					R.layout.item_files_shared_list, null, false);
			
			holder = new ViewHolder();
			holder.friendSharedImg = (ImageView) convertView.findViewById(
					R.id.friend_shared_item_img);
			holder.friendSharedName = (TextView) convertView.findViewById(
					R.id.files_shared_item_name);
			holder.friendSharedSelection = (CheckBox) convertView.findViewById(
					R.id.files_shared_item_select);
			
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.friendSharedName.setText(friendSharedData.
				get(position).getFriendName());
		
		holder.friendSharedSelection.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				friendSharedBooleanArr.put(position, isChecked);
			}
		});
		
		boolean isChecked = friendSharedBooleanArr.get(position);
		holder.friendSharedSelection.setChecked(isChecked);
		
		holder.friendSharedImg.setImageDrawable(RandomImageUtil.getFriendImg(activity));
		
		return convertView;
	}
	
	private class ViewHolder {
		
		ImageView friendSharedImg;
		
		TextView friendSharedName;
		
		CheckBox friendSharedSelection;
		
	}
	
	/**
	 * 设置是否选择所有好友
	 * @param isAllChecked
	 */
	public void setAllChecked(boolean isAllChecked) {
		for (int i = 0; i < friendSharedData.size(); i++) {
			friendSharedBooleanArr.put(i, isAllChecked);
		}
		notifyDataSetChanged();
	}
	
	/**
	 * 获取好友列表选择列表
	 * @return
	 */
	public List<Integer> getCheckedFriendSharedList() {
		List<Integer> checkedFriendSharedList = new ArrayList<Integer>();
		for (int i = 0; i < friendSharedData.size(); i++) {
			if (friendSharedBooleanArr.get(i)) {
				checkedFriendSharedList.add(i);
			}
		}
		return checkedFriendSharedList;
	}
	
	/**
	 * 设置数据
	 * @param friendSharedData
	 */
	public void setData(List<FriendInfoBean> friendSharedData) {
		this.friendSharedData = friendSharedData;
		notifyDataSetInvalidated();
	}

}
